package common.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import model.dap.dao.CityDAO;
import model.dap.dao.DealCategoryDAO;
import model.dap.dao.DistrictDAO;
import model.dap.dao.LogDAO;
import model.dap.domain.City;
import model.dap.domain.Consumer;
import model.dap.domain.Deal;
import model.dap.domain.DealCategory;
import model.dap.domain.District;
import model.dap.domain.Gender;
import model.dap.domain.Log;
import model.dap.mapper.CityMapper;
import model.dap.mapper.DealCategoryMapper;
import model.dap.mapper.DistrictMapper;
import model.dap.mapper.LogMapper;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import common.util.Consts;

public abstract class BaseAction extends ActionSupport implements SessionAware {

	private Map<String, Object> _session;
	protected ArrayList<String> arrErrorMessage;

	public ArrayList<String> getArrErrorMessage() {
		return arrErrorMessage;
	}

	public void setArrErrorMessage(ArrayList<String> arrErrorMessage) {
		this.arrErrorMessage = arrErrorMessage;
	}

	// Chinh tien khi hien
	public String newMoney(String oldMoney) {
		String[] results = oldMoney.split("\\.");
		int lg = results[0].length();
		int count = 0;
		String newMonney = "";
		for (int i = lg - 1; i >= 0; i--) {
			if (count < 3) {
				newMonney = results[0].charAt(i) + newMonney;
				count += 1;
			} else {
				if (i != 0) {
					newMonney = results[0].charAt(i) + "." + newMonney;
					count = 1;
				} else {
					newMonney = results[0].charAt(i) + "." + newMonney;
				}
			}
		}
		return newMonney;
	}

	// ham insert vao bang log
	public int dealID;

	// truong hop ghi log khi xem detail

	public String insertLogV(int dealID) {
		try {
			insertlog.setUserID(getCurrentUser().getUserID());
			LogMapper logmap = new LogDAO();

			insertlog.setDealID(dealID);
			int result = logmap.insertLogV(insertlog);
			if (result == 0) {
				return ERROR;
			}
			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}
	}

	// truong hop ghi log khi order
	public String insertLogO() {
		try {

			LogMapper logmap = new LogDAO();
			insertlog.setUserID(getCurrentUser().getUserID());
			insertlog.setDealID(getSelectedDealID());
			int result = logmap.insertLogO(insertlog);
			if (result == 0) {
				return ERROR;
			}

			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}
	}

	public Deal deal = new Deal();
	public Log insertlog = new Log();
	protected int ItemPerPage = 15;
	private int Offset = 1;
	public int count;

	protected int numberPage = Offset + 1;
	protected String totalPage;
	protected String contextPath = null;
	private static String nextPage = null;
	private static int selectedDealID = 0;

	private String successmessage;

	public String getSuccessmessage() {
		return successmessage;
	}

	public void setSuccessmessage(String successmessage) {
		this.successmessage = successmessage;
	}

	public static int getSelectedDealID() {
		return selectedDealID;
	}

	public static void setSelectedDealID(int selectedDealID) {
		BaseAction.selectedDealID = selectedDealID;
	}

	public static String getNextPage() {
		return nextPage;
	}

	public static void setNextPage(String nextPages) {
		nextPage = nextPages;
	}

	public String getContextPath() {
		contextPath = ServletActionContext.getRequest().getContextPath();
		return contextPath;
	}

	public int getItemPerPage() {
		return ItemPerPage;
	}

	public void setItemPerPage(int itemPerPage) {
		ItemPerPage = itemPerPage;
	}

	public int getOffset() {
		return Offset;
	}

	public void setOffset(int offset) {
		Offset = offset;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getNumberPage() {
		return numberPage;
	}

	public void setNumberPage(int numberPage) {
		this.numberPage = numberPage;
	}

	public String getTotalPage() {
		return totalPage;
	}

	// ======================================================================================================
	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	// ======================================================================================================

	protected ArrayList<City> city, city2;
	protected ArrayList<District> district;
	protected String citySelected, districtSelected, genderSelected;
	protected String citySelected2;

	public String getGenderSelected() {
		return genderSelected;
	}

	public void setGenderSelected(String genderSelected) {
		this.genderSelected = genderSelected;
	}

	// gender
	private List<Gender> genders = new ArrayList<Gender>();

	public List<Gender> getGenders() {
		return genders;
	}

	public void setGenders(List<Gender> genders) {
		this.genders = genders;
	}

	public void createGenderList() {
		Gender genderNam = new Gender();
		genderNam.setGenderID("Nam");
		genderNam.setGenderName("Nam");

		genders.add(genderNam);

		Gender genderNu = new Gender();
		genderNu.setGenderID("Nữ");
		genderNu.setGenderName("Nữ");

		genders.add(genderNu);

		Gender genderKhac = new Gender();
		genderKhac.setGenderID("Khác");
		genderKhac.setGenderName("Khác");

		genders.add(genderKhac);

	}

	public List<City> getCity2() {
		return city2;
	}

	public void setCity2(ArrayList<City> city2) {
		this.city2 = city2;
	}

	public String getCitySelected2() {
		return citySelected2;
	}

	public void setCitySelected2(String citySelected2) {
		this.citySelected2 = citySelected2;
	}

	public ArrayList<City> getCity() {
		return city;
	}

	public void setCity(ArrayList<City> city) {
		this.city = city;
	}

	public ArrayList<District> getDistrict() {
		return district;
	}

	public void setDistrict(ArrayList<District> district) {
		this.district = district;
	}

	public String getCitySelected() {
		return citySelected;
	}

	public void setCitySelected(String citySelected) {
		this.citySelected = citySelected;
	}

	public String getDistrictSelected() {
		return districtSelected;
	}

	public void setDistrictSelected(String districtSelected) {
		this.districtSelected = districtSelected;
	}

	public String getDefaultSearchEngine() {
		return "HỒ CHÍ MINH";
	}

	public void getCityList() {
		CityMapper cityMapper = new CityDAO();
		city = cityMapper.selectCityList();
	}

	public void getCityList2() {
		CityMapper cityMapper = new CityDAO();
		city2 = cityMapper.selectCityList2();
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute("sessionID") == null)
			session.setAttribute("sessionID", "2");
	}

	public void getDistrictList(int cityid) {
		DistrictMapper districtMapper = new DistrictDAO();
		district = districtMapper.selectDistrictFromCity(cityid);
	}

	// ======================================================================================================
	private List<DealCategory> parentDealCategory;

	public List<DealCategory> getParentDealCategory() {
		return parentDealCategory;
	}

	public void setParentDealCategory(List<DealCategory> parentDealCategory) {
		this.parentDealCategory = parentDealCategory;
	}

	// ======================================================================================================
	// GET PARENT DEAL CATEGORY
	public void loadParentDealCategory() {
		DealCategoryMapper dealCategoryMapper = new DealCategoryDAO();
		parentDealCategory = dealCategoryMapper.selectDealCategory(18);
		for (DealCategory parent : parentDealCategory) {
			loadChildDealCategory(parent);
		}
	}

	// ======================================================================================================
	// GET CHILD DEAL CATEGORY
	public void loadChildDealCategory(DealCategory parent) {
		DealCategoryMapper dealCategoryMapper = new DealCategoryDAO();
		parent.setChildDealCategory(dealCategoryMapper
				.selectDealCategory(parent.getCategoryID()));
	}

	private static final long serialVersionUID = 1L;

	protected void showError(String Message) {
		arrErrorMessage.add(Message);
	}

	public BaseAction() {
		arrErrorMessage = new ArrayList<String>();
		_session = null;
	}

	public Consumer getCurrentUser() {
		return (Consumer) _session.get(Consts.SESSION_LOING_MASTER_USER);
	}

	protected void setCurrentUser(Consumer user) {
		if (_session != null && user != null) {
			_session.put(Consts.SESSION_LOING_MASTER_USER, user);
		}
	}

	protected void delCurrentUser() {
		_session.put(Consts.SESSION_LOING_MASTER_USER, null);
	}

	@Override
	public void setSession(Map<String, Object> session) {
		_session = session;
	}

	//SESSION CITY
	protected void setCurrentCityID(String cityID) {
		if (_session != null && cityID != null) {
			_session.put(Consts.SESSION_CITY, cityID);
		}
	}
	
	public String getCurrentCityID() {
		return (String) _session.get(Consts.SESSION_CITY);
	}

	protected void delCurrentCityID() {
		_session.put(Consts.SESSION_CITY, null);
	}
	
	//SESSION CATEGORY
	protected void setCurrentCategoryID(String categoryID) {
		if (_session != null && categoryID != null) {
			_session.put(Consts.SESSION_CATEGORY, categoryID);
		}
	}
	
	public String getCurrentCategoryID() {
		return (String) _session.get(Consts.SESSION_CATEGORY);
	}

	protected void delCurrentCategoryID() {
		_session.put(Consts.SESSION_CATEGORY, null);
	}
	
	public Deal getDeal() {
		return deal;
	}

	public void setDeal(Deal deal) {
		this.deal = deal;
	}

	/*
	 * public String getDealTempID() { return dealTempID; }
	 * 
	 * public void setDealTempID(String dealTempID) { this.dealTempID =
	 * dealTempID; }
	 */
	protected int sessionDealID;

	public int getSessionDealID() {
		return sessionDealID;
	}

	public void setSessionDealID(int sessionDealID) {
		this.sessionDealID = sessionDealID;
	}

}